package com.prs.crm.action.purchase;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.purchase.OrderType;
import com.prs.crm.service.purchase.OrderTypeService;
import com.prs.crm.service.sys.LogService;



public class OrderTypeAction extends BaseAction {

	private static final long serialVersionUID = 8617200811428319767L;
	
	private OrderType orderType;
	private Collection<OrderType> orderTypes;
	private OrderTypeService orderTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;
	private String orderTypeName;
	
	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	public String getOrderTypeName() {
		return orderTypeName;
	}
	public void setOrderTypeName(String orderTypeName) {
		this.orderTypeName = orderTypeName;
	}
	@Action("editOrderType")
	public String editOrderType(){
		return SUCCESS;
	}
	@Action("listOrderType")
	public String listOrderType(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(OrderType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOrderTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action(value="saveOrderType",results={@Result(name="success",type="redirect",location="listOrderType"),
			@Result(name="error",type="chain",location="editOrderType")})
	public String saveOrderType(){
		if(orderType.getName()==null||orderType.getName().equals("")){
			this.addActionError("订单类型不能为空");
			return ERROR;
		}
		if(this.getLogService().getSearchName(OrderType.class, orderType.getName())){
			if(!(orderTypeName.equals(orderType.getName()))){
				this.addActionError("名称已存在");
				return ERROR;
			}
		}
		this.getOrderTypeService().save(orderType);
		return SUCCESS;
	}
	
	@Action(value="removeOrderType",results={@Result(name="success",type="redirect",location="listOrderType")})
	public String removeOrderType(){
		this.getOrderTypeService().batchRemove(ids);
		return SUCCESS;
	}
	
	
	public OrderType getOrderType() {
		return orderType;
	}
	public void setOrderType(OrderType orderType) {
		this.orderType = orderType;
	}
	public OrderTypeService getOrderTypeService() {
		return orderTypeService;
	}
	public void setOrderTypeService(OrderTypeService orderTypeService) {
		this.orderTypeService = orderTypeService;
	}
	public Collection<OrderType> getOrderTypes() {
		return orderTypes;
	}
	public void setOrderTypes(Collection<OrderType> orderTypes) {
		this.orderTypes = orderTypes;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	
	
}
